<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Advanced GdaDataSelect usage: GNOME Data Access 5 manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GNOME Data Access 5 manual">
<link rel="up" href="data_models.html" title="Data handling">
<link rel="prev" href="libgda-5.0-Gda-Value.html" title="A single Value">
<link rel="next" href="GdaBlobOp.html" title="Blobs">
<meta name="generator" content="GTK-Doc V1.32 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="data_models.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="libgda-5.0-Gda-Value.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="GdaBlobOp.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="data-select"></a>Advanced GdaDataSelect usage</h2></div></div></div>
<p>
    Whenever a SELECT statement is successfully executed (using the <a class="link" href="GdaConnection.html" title="GdaConnection">GdaConnection</a>'s methods),
    a new <a class="link" href="GdaDataSelect.html" title="GdaDataSelect">GdaDataSelect</a> object is created, which can be used as any other
    <a class="link" href="GdaDataModel.html" title="GdaDataModel">GdaDataModel</a> object. However this object has some extra features which are
    described in this section.
  </p>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="data-select-rerun"></a>Automatic re-run of the SELECT statement</h3></div></div></div>
<p>
      If the SELECT statement which has been executed contained some parameters, then the 
      <a class="link" href="GdaDataSelect.html#GdaDataSelect--auto-reset" title="The “auto-reset” property">auto-reset</a> property controls whether the
      <a class="link" href="GdaDataSelect.html" title="GdaDataSelect">GdaDataSelect</a> object should re-run the SELECT statement
      to have an up-to-date contents. This feature is disabled by default but can be enabled anytime.
    </p>
<p>
      For example the following code (errors checking omitted for readability):
      </p>
<pre class="programlisting">
GdaStatement *stmt;
GdaSqlParser *parser = ...;
GdaDataModel *model;

stmt = gda_sql_parser_parse_string (parser,
       "SELECT * FROM customers WHERE id &lt;= ##theid::gint",
       NULL, NULL);
gda_statement_get_parameters (stmt, &amp;params, NULL);
gda_set_set_holder_value (params, NULL, "theid", 9);
model = gda_connection_statement_execute_select (cnc, stmt, params, NULL);
g_object_set (G_OBJECT (model), "auto-reset", TRUE, NULL);
g_object_unref (stmt);
      </pre>
<p>
      would create a <a class="link" href="GdaDataSelect.html" title="GdaDataSelect">GdaDataSelect</a> object (the 'model' variable) with the
      following contents:
      </p>
<pre class="programlisting">
id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR 
 9 | Greg Popoff    |                 2 | SP      | MDR 
      </pre>
<p>
      and with the following changes:
      </p>
<pre class="programlisting">
gda_set_set_holder_value (params, NULL, "theid", 4);
      </pre>
<p>
      the contents of the data model will automatically be set to:
      </p>
<pre class="programlisting">
id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR
      </pre>
<p>
    </p>
<p>
      Important note: with some database providers (such as SQLite), the column's types (if not specified when the statement
      is run) cannot be determined untill there is a value in the column. This means that a column's type may change
      over time from the GDA_TYPE_NULL type to its correct type.
    </p>
</div>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="data-select-empty-rs"></a>Invalid parameters</h3></div></div></div>
<p>
      If the SELECT statement which has been executed contained some parameters, and if it is not possible to
      give correct values to the parameters when the data model resulting from the execution of the SELECT must be
      created, then the execution should fail and no data model should be created (see the
      <a class="link" href="GdaConnection.html#gda-connection-statement-execute" title="gda_connection_statement_execute ()">gda_connection_statement_execute()</a>'s documentation).
      However that default behaviour can be changed using the GDA_STATEMENT_MODEL_ALLOW_NOPARAM flag: the returned
      data model will have no row and will automatically update its contents (re-run the SELECT statement)
      when parameters are changed.
    </p>
<p>
      The example above can be modified as follows, note that the value of the 'theid' parameter is not set
      which makes it invalid:
      </p>
<pre class="programlisting">
	GdaStatement *stmt;
GdaSqlParser *parser = ...;
GdaDataModel *model;

stmt = gda_sql_parser_parse_string (parser, 
       "SELECT * FROM customers WHERE id &lt;= ##theid::gint",
        NULL, NULL);
gda_statement_get_parameters (stmt, &amp;params, NULL);
model = gda_connection_statement_execute_select_full (cnc, stmt, params,
                                                      GDA_STATEMENT_MODEL_ALLOW_NOPARAM,
                                                      NULL, NULL);
g_object_unref (stmt);
      </pre>
<p>
      The created data model contains no row, and with the following changes:
      </p>
<pre class="programlisting">
gda_set_set_holder_value (params, NULL, "theid", 4);
      </pre>
<p>
      the contents of the data model will automatically be set to:
      </p>
<pre class="programlisting">
id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR
      </pre>
<p>
    </p>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.32</div>
</body>
</html>